<script>
import PostBase from '@/components/post/PostBase.vue'
import { apiGetAbout } from '@/api/topic.js'
export default {
  name: 'TopicList',
  extends: PostBase,
  props: {
    topicName: {
      type: String,
      required: true
    },
    topicId: {
      type: Number
    }
  },
  data () {
    return {
      platform: 'h5',
      sortby: 2,
      offset: null
    }
  },
  watch: {
    topicName: {
      handler (newVal) {
        this.resetListStatus()
        this.source = []
        this.offset = null
        this.getData()
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    getData () {
      apiGetAbout({
        topicName: this.topicName,
        topicId: this.topicId,
        sortby: this.sortby,
        platform: this.platform,
        offset: this.offset
      }).then(res => {
        const { code, data } = res
        if (code === 0 && data) {
          const { cards } = data
          this.offset = cards[cards.length - 1].desc.dynamic_id_str
          this.source.push(...cards)
          if (data.has_more !== 0) {
            this.listFinished = false
          } else {
            this.listFinished = true
          }
        } else {
          this.listFinished = true
        }
      }).catch(err => {
        console.log(err)
        this.listError = true
      }).finally(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
